//
//  EmergencyViewController.m
//  安好
//
//  Created by 马晓良 on 14-12-4.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "EmergencyViewController.h"
#import "JiZhenDetailViewController.h"
#import "SystemSettingTableViewCell.h"
@interface EmergencyViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger emergencyPages;
}
@property (strong, nonatomic) IBOutlet UITableView *emergencyTableView;
@property (nonatomic, strong) NSMutableArray *emergencyArray;
@end

@implementation EmergencyViewController

- (NSMutableArray *)emergencyArray{
    if (_emergencyArray == nil) {
        self.emergencyArray = [NSMutableArray array];
    }
    return _emergencyArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    emergencyPages = 1;
    self.emergencyTableView.dataSource = self;
    self.emergencyTableView.delegate = self;
    self.emergencyTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self loadData];
    [self setNaviBarAttribute];
    [self.emergencyTableView addFooterWithTarget:self action:@selector(loadData)];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置naviBar 并实现左右naviBar button方法 -
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationItem.title = @"我的急症";
}


/**
 *  返回主页面
 */
- (void)handleLeftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableView的代理方法 -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.emergencyArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (kScreenWidth == 414) {
        return 54;
    }else{
        return 44;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [self.emergencyArray[indexPath.row] objectForKey:@"eme"];
    
    cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
    if (kScreenWidth == 414) {
        UIView *bottomLineView = [[UIView alloc] init];
        if ([[self.emergencyArray.lastObject objectForKey:@"eme"] isEqualToString:cell.textLabel.text]) {
            bottomLineView.frame = CGRectMake(0, 53,kScreenWidth, 0.5);
        }else{
            bottomLineView.frame = CGRectMake(15, 53,kScreenWidth-15, 0.5);
        }
        bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        [cell addSubview:bottomLineView];
    }else{
        UIView *bottomLineView = [[UIView alloc]init];
        if ([[self.emergencyArray.lastObject objectForKey:@"eme"] isEqualToString:cell.textLabel.text]) {
            bottomLineView.frame = CGRectMake(0, cell.frame.size.height-1,kScreenWidth, 0.5);
        }else{
            bottomLineView.frame = CGRectMake(15, cell.frame.size.height-1,kScreenWidth-15, 0.5);
        }
        bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        [cell addSubview:bottomLineView];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JiZhenDetailViewController *jiZhenDetailVC = [[JiZhenDetailViewController alloc] init];
    jiZhenDetailVC.emeId = [self.emergencyArray[indexPath.row] objectForKey:@"emeid"];
    jiZhenDetailVC.emeName = [self.emergencyArray[indexPath.row] objectForKey:@"eme"];
    [self.navigationController pushViewController:jiZhenDetailVC animated:YES];
}


#pragma mark - 数据加载
-(void)loadData
{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    [dict setObject:@"8" forKey:@"type"];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)emergencyPages] forKey:@"page"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@collection/list/",kBaseURL] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
            if (self.emergencyArray.count == 0) {
                [self shouLabel:@"暂无内容"];
            }
            [self.emergencyTableView footerEndRefreshing];
        }else{
            NSArray *jsonDictArr = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
            
            MyNSLog("%@", jsonDictArr);
            if (jsonDictArr != nil) {
                emergencyPages++;
                for (NSDictionary * dic in jsonDictArr){
                    [self.emergencyArray addObject:dic];
                }
                [self.emergencyTableView reloadData];
            }else if ([jsonDictArr count] < 20){
                [self.emergencyTableView footerEndRefreshing];
            }else{
                if (emergencyPages>1) {
                    MyNSLog("下拉无数据");
                }
            }
            
        }
        [self.emergencyTableView footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        [self.emergencyTableView footerEndRefreshing];
    }];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
